package by.dvrudenko.avtobaza.dataaccess.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.commons.lang3.Validate;
import org.hibernate.jpa.criteria.OrderImpl;
import org.springframework.stereotype.Repository;

import by.dvrudenko.avtobaza.dataaccess.DriverDao;
import by.dvrudenko.avtobaza.datamodel.Car;
import by.dvrudenko.avtobaza.datamodel.Driver;
import by.dvrudenko.avtobaza.datamodel.Driver_;
import by.dvrudenko.avtobaza.datamodel.Route_;

@Repository
public class DriverDaoImpl extends AbstractDaoImpl<Long, Driver> implements
		DriverDao {

	public DriverDaoImpl() {
		super(Driver.class);
	}


	 @Override
	    public List<Driver> getAllDriver() {
	        CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

	        CriteriaQuery<Driver> criteria = cBuilder.createQuery(Driver.class);
	        Root<Driver> root = criteria.from(Driver.class);

	        criteria.select(root);

	        TypedQuery<Driver> query = getEm().createQuery(criteria);
	        List<Driver> results = query.getResultList();
	        return results;
	    }
	 
	    @Override
	    public Long getCount() {
	        CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

	        CriteriaQuery<Long> criteria = cBuilder.createQuery(Long.class);
	        Root<Driver> root = criteria.from(Driver.class);

	        criteria.select(cBuilder.count(root));

	        TypedQuery<Long> query = getEm().createQuery(criteria);
	        return query.getSingleResult();
	    }

	    @Override
	    public List<Driver> getAllDriver(SingularAttribute<Driver, ?> attr, boolean ascending, int startRecord, int pageSize){
	        CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

	        CriteriaQuery<Driver> criteria = cBuilder.createQuery(Driver.class);
	        Root<Driver> root = criteria.from(Driver.class);

	        criteria.select(root);
	        criteria.orderBy(new OrderImpl(root.get(attr), ascending));

	        TypedQuery<Driver> query = getEm().createQuery(criteria);
	        query.setFirstResult(startRecord);
	        query.setMaxResults(pageSize);

	        List<Driver> results = query.getResultList();
	        return results;
	    }


		@Override
		public Driver getDriverByUserId(Long id) {
			Validate.notNull(id, "Id - attributes can't be null");
	        CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();
	        CriteriaQuery<Driver> criteria = cBuilder.createQuery(Driver.class);
	        Root<Driver> root = criteria.from(Driver.class);
			criteria.where(cBuilder.equal(root.get(Driver_.user), id));			
	        TypedQuery<Driver> query = getEm().createQuery(criteria);
			
	        Driver driver = query.getSingleResult();
			
			return driver;
		}
	    
	    
}
